import CryptoJS from "crypto-js"
export const AES_DEFAULT_KEY = CryptoJS.enc.Utf8.parse(import.meta.env.AES_KEY)
/**
 * AES 加密
 * KEY: // 需要前后端保持一致
 * mode: ECB // 需要前后端保持一致
 * pad: Pkcs7 //前端 Pkcs7 对应 后端 Pkcs5
 * @param plaintext
 */
export const aesEncrypt = (plaintext:any) => {
    return CryptoJS.AES.encrypt(plaintext, AES_DEFAULT_KEY, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString()
}
/**
 * AES 加密
 * KEY: // 需要前后端保持一致
 * mode: ECB // 需要前后端保持一致
 * pad: Pkcs7 //前端 Pkcs7 对应 后端 Pkcs5
 * @param plaintext
 * @param key
 */
export const aesEncryptKey = (plaintext:any,key:string) => {
    return CryptoJS.AES.encrypt(plaintext, CryptoJS.enc.Utf8.parse(key), {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString()
}
/**
 * AES 解密
 * @param jsonStr
 */
export const aesDecrypt = (jsonStr:string) => {
    return CryptoJS.AES.decrypt(jsonStr, AES_DEFAULT_KEY, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8)
}
/**
 * AES 解密
 * @param jsonStr
 * @param key
 */
export const aesDecryptKey = (jsonStr:string,key:string) => {
    return CryptoJS.AES.decrypt(jsonStr, CryptoJS.enc.Utf8.parse(key), {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8)
}
